var zcHttp = {
	KeyHttp: 'zcsoft_uni_partner_httpurl',
	KeySession: 'zcsoft_uni_partner_session',
	ServerUrl: '',
	WebSession: '',
	IsNeedLogin: 'true',
	getConfig: function (options) {
		let me = this;
		if(options.IsNeedLogin === false || options.IsNeedLogin === 'false')
			me.IsNeedLogin = 'false';
		else
			me.IsNeedLogin = 'true';
			
		// #ifdef MP-WEIXIN
		me.WebSession = uni.getStorageSync(me.KeySession);
		me.ServerUrl = uni.getStorageSync(me.KeyHttp) + '?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();
		// #endif
		
	},
	doCheck: function (options) {
		let me = this;
		if (!me.ServerUrl) {
			// 要跳转到错误页,显示请配置服务器访问地址。
		}
	},
	Post: function (options) {
        try{
            let me = this;
            me.getConfig(options);
            me.doCheck(options);
            let PlateType = '';
            // #ifdef MP-WEIXIN
            PlateType = 'WxMP';
            // #endif
			let reqPara = {};
            // 网络请求
            options.Paras.session = me.WebSession;
			if(options.Scope.$ZcPublic.WaitModel != '0') {
				// 显示加载动画
				uni.showLoading({
				    title: '处理中',
					mask:true
				});
			}
			if(options.Scope.$ZcPublic.IsPerLog == 'true') {
				// 写性能日志
				reqPara.Name = options.Paras.Name;
				reqPara.Action = options.Paras.Action;
				reqPara.Start = me.getNowTime();
				reqPara.Data = options.Paras.Data;
			}
            uni.request({
              url: me.ServerUrl,
              data: options.Paras,
              method:"POST",
              dataType:"json",
              success: function (response) {
            	if(response.statusCode === 200) {
					if (response.data.msg == '请重新登录!') {
					      uni.showModal({
					          title: '温馨提示',
					          content: ''+response.data.msg,
					          showCancel: false,
					          success: res => {
					              uni.removeStorageSync(me.KeySession)
					              wx.redirectTo({
					                  url:"../login/login?isLoading=false&isLogin=true"
					              })
					          }
					      });
					      return;
					}
            		if(PlateType == 'WxMP'){
            			if(response && response.data && response.data.success && response.data.session) {
            				uni.setStorageSync(me.KeySession,response.data.session);
            			}
            		}
					if(options.Callback) {
						options.Callback(response.data,options.Scope);
					}
            	}
              },
              fail: function (response){
            	  if(response.errMsg){
            		  if(response.errMsg.toLowerCase().trim() === 'request:fail timeout') {
            			  uni.showModal({
            			      title: '温馨提示',
            			      content: '网络请求超时:服务器可能过于繁忙,请稍候再试!',
            				  showCancel: false
            			  });
            		  }
					  if(response.errMsg.toLowerCase().trim() === 'request:fail') {
						  uni.showModal({
							  title: '温馨提示',
							  content: '网络状态不正常:当前网络无法连接到服务器,请稍候再试!',
							  showCancel: false
						  });
					  }
            	  }
              },
              complete:function(response) {
				  if(options.Scope.$ZcPublic.WaitModel != '0') {
					  uni.hideLoading();
				  }
				  let isNetworkOk = true; //网络是否通畅
				  if(response.errMsg){
					  if(response.errMsg.toLowerCase().trim() === 'request:fail timeout') {
						  isNetworkOk = false;
					  }
					  if(response.errMsg.toLowerCase().trim() === 'request:fail'){
						  isNetworkOk = false;
					  }
				  }
				  if(isNetworkOk && options.Scope.$ZcPublic.IsPerLog == 'true') {
				  	// 写性能日志
				  	reqPara.End = me.getNowTime();
					let reqJsonStr = JSON.stringify(reqPara);
					let _ServerUrl = uni.getStorageSync(me.KeyHttp) + '?IsNeedLogin=false&dc=' + me.getDc();
 					uni.request({
						url: _ServerUrl,
						data: {
							Name:'Uni_MpLog',
							Action: 'setMpLog',
							Data:{
								StrData :reqJsonStr
							}
						},
						method:"POST",
						dataType:"json",
						fail: function (resp){},
						complete:function(resp){}
					}); 
				  }
              }
            });
        }catch(e){
            console.error('错误 = ',e);
        }
	},
	ButtonPost: function (options) {
	    try{
	        let me = this;
	        me.getConfig(options);
	        me.doCheck(options);
	        let PlateType = '';
	        // #ifdef MP-WEIXIN
	        PlateType = 'WxMP';
	        // #endif
			let reqPara = {};
	        // 网络请求
	        options.Paras.session = me.WebSession;
			if(options.Scope.$ZcPublic.WaitModel != '0') {
				if(options.WaitModel != '2') {
					// 显示加载动画
					uni.showLoading({
					    title: '处理中',
						mask:true
					});
				}
			}
			if(options.Scope.$ZcPublic.IsPerLog == 'true') {
				// 写性能日志
				reqPara.Name = options.Paras.Name;
				reqPara.Action = options.Paras.Action;
				reqPara.Start = me.getNowTime();
				reqPara.Data = options.Paras.Data;
			}
	        uni.request({
	          url: me.ServerUrl,
	          data: options.Paras,
	          method:"POST",
	          dataType:"json",
	          success: function (response) {
	        	if(response.statusCode === 200) {
					if (response.data.msg&&response.data.msg.indexOf('你当前不是销售代表无法处理相关业务') >= 0) {
					      uni.showModal({
					          title: '温馨提示',
					          content: ''+response.data.msg,
					          showCancel: false,
					          success: res => {
					              uni.removeStorageSync(me.KeySession)
					              wx.redirectTo({
					                  url:"../login/login?isLoading=false&isLogin=true"
					              })
					          }
					      });
					      return;
					}
	        		if(PlateType == 'WxMP') {
	        			if(response && response.data && response.data.success && response.data.session) {
	        				uni.setStorageSync(me.KeySession,response.data.session);
	        			}
	        		}
					if(options.Fn_Success) {
						options.Fn_Success(response.data,options.Scope);
					}
	        	}
	          },
	          fail: function (response){
	        	  if(response.errMsg){
	        		  if(response.errMsg.toLowerCase().trim() === 'request:fail timeout') {
	        			  uni.showModal({
	        			      title: '温馨提示',
	        			      content: '网络请求超时:服务器可能过于繁忙,请稍候再试!',
	        				  showCancel: false
	        			  });
	        		  }
					  if(response.errMsg.toLowerCase().trim() === 'request:fail') {
						  uni.showModal({
							  title: '温馨提示',
							  content: '网络状态不正常:当前网络无法连接到服务器,请稍候再试!',
							  showCancel: false
						  });
					  }
	        	  }
	          },
	          complete:function(response) {
				  if(options.Scope.$ZcPublic.WaitModel != '0') {
					  if(options.WaitModel != '2') {
						uni.hideLoading();
					  }
				  }
				  let isNetworkOk = true; //网络是否通畅
				  if(response.errMsg){
					  if(response.errMsg.toLowerCase().trim() === 'request:fail timeout') {
						  isNetworkOk = false;
					  }
					  if(response.errMsg.toLowerCase().trim() === 'request:fail'){
						  isNetworkOk = false;
					  }
				  }
				  if(options.Scope.$ZcPublic.WaitModel != '0') {
					  if(options.WaitModel == '2') {
						  if(options.Fn_Complete) {
							options.Fn_Complete();
						  }
					  }
				  }
				  if(isNetworkOk && options.Scope.$ZcPublic.IsPerLog == 'true') {
				  	// 写性能日志
				  	reqPara.End = me.getNowTime();
					let reqJsonStr = JSON.stringify(reqPara);
					let _ServerUrl = uni.getStorageSync(me.KeyHttp) + '?IsNeedLogin=false&dc=' + me.getDc();
					uni.request({
						url: _ServerUrl,
						data: {
							Name:'Uni_MpLog',
							Action: 'setMpLog',
							Data:{
								StrData :reqJsonStr
							}
						},
						method:"POST",
						dataType:"json",
						fail: function (resp){},
						complete:function(resp){}
					}); 
				  }
	          }
	        });
	    }catch(e){
	        console.error('错误 = ',e);
	    }
	},
	Log:function (page, func, msg, scope) {
		let me = this;
		let desc = '';
		
		uni.showModal({
		    title: '错误提示',
		    content: msg,
			showCancel: false
		});
		if (scope.$ZcPublic.IsErrLog == 'true') {
			// #ifdef MP-WEIXIN
			let IsCanSysInfo = uni.canIUse('getSystemInfoSync.return.screenWidth');
			if(IsCanSysInfo){
				let curSystem = uni.getSystemInfoSync();
				desc += '品牌:' + curSystem.brand;
				desc += ', 型号:' + curSystem.model;
				desc += ', 像素比:' + curSystem.pixelRatio;
				desc += ', 屏宽:' + curSystem.screenWidth;
				
				desc += ', 屏高:' + curSystem.screenHeight;
				desc += ', 系统版本:' + curSystem.system;
				desc += ', 微信版本:' + curSystem.version;
				desc += ', 平台:' + curSystem.platform;
			}
			// #endif
			let _ServerUrl = uni.getStorageSync(me.KeyHttp) + '?IsNeedLogin=false&dc=' + me.getDc();
			uni.request({
				url: _ServerUrl,
				data: {
					Name:'Uni_WxLog',
					Action: 'setWxLog',
					Data: {
						Page: page,
						Func: func,
						Msg: msg,
						Desc:desc,
						Data:'-'
					}
				},
				method:"POST",
				dataType:"json",
				fail: function (resp){},
				complete:function(resp){}
			}); 
		}
	},
	getDc: function () {
	  return new Date().getTime();
	},
	getNowTime: function (){
		let now = new Date();
		let year = now.getFullYear();
		let month = now.getMonth() + 1;
		let day = now.getDate();
		let hour = now.getHours();
		let mins = now.getMinutes();
		let secs = now.getSeconds();
		let minis = now.getMilliseconds();
		return year + '-' + month + '-' + day + ' ' + hour + ':' + mins + ':' + secs + '.' + minis;
	},
    Base64: function() {
            // 数据加密算法 封装方法 
            _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
            // public method for encoding  
            this.encode = function (input) {
                let output = "";
                let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
                let i = 0;
                input = _utf8_encode(input);
                while (i < input.length) {
                    chr1 = input.charCodeAt(i++);
                    chr2 = input.charCodeAt(i++);
                    chr3 = input.charCodeAt(i++);
                    enc1 = chr1 >> 2;
                    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                    enc4 = chr3 & 63;
                    if (isNaN(chr2)) {
                        enc3 = enc4 = 64;
                    } else if (isNaN(chr3)) {
                        enc4 = 64;
                    }
                    output = output +
                        _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                        _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
                }
                return output;
            }
    
            // public method for decoding  
            this.decode = function (input) {
                let output = "";
                let chr1, chr2, chr3;
                let enc1, enc2, enc3, enc4;
                let i = 0;
                input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
                while (i < input.length) {
                    enc1 = _keyStr.indexOf(input.charAt(i++));
                    enc2 = _keyStr.indexOf(input.charAt(i++));
                    enc3 = _keyStr.indexOf(input.charAt(i++));
                    enc4 = _keyStr.indexOf(input.charAt(i++));
                    chr1 = (enc1 << 2) | (enc2 >> 4);
                    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                    chr3 = ((enc3 & 3) << 6) | enc4;
                    output = output + String.fromCharCode(chr1);
                    if (enc3 != 64) {
                        output = output + String.fromCharCode(chr2);
                    }
                    if (enc4 != 64) {
                        output = output + String.fromCharCode(chr3);
                    }
                }
                output = _utf8_decode(output);
                return output;
            }
    
            // private method for UTF-8 encoding  
            _utf8_encode = function (string) {
                string = string.replace(/\r\n/g, "\n");
                let utftext = "";
                for (let n = 0; n < string.length; n++) {
                    let c = string.charCodeAt(n);
                    if (c < 128) {
                        utftext += String.fromCharCode(c);
                    } else if ((c > 127) && (c < 2048)) {
                        utftext += String.fromCharCode((c >> 6) | 192);
                        utftext += String.fromCharCode((c & 63) | 128);
                    } else {
                        utftext += String.fromCharCode((c >> 12) | 224);
                        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                        utftext += String.fromCharCode((c & 63) | 128);
                    }
    
                }
                return utftext;
            }
    
            // private method for UTF-8 decoding  
            _utf8_decode = function (utftext) {
                let string = "";
                let i = 0;
                let c = c1 = c2 = 0;
                while (i < utftext.length) {
                    c = utftext.charCodeAt(i);
                    if (c < 128) {
                        string += String.fromCharCode(c);
                        i++;
                    } else if ((c > 191) && (c < 224)) {
                        c2 = utftext.charCodeAt(i + 1);
                        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                        i += 2;
                    } else {
                        c2 = utftext.charCodeAt(i + 1);
                        c3 = utftext.charCodeAt(i + 2);
                        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                        i += 3;
                    }
                }
                return string;
            }
        }
}

export default zcHttp;

/* 调用方式
import zcHttp from 'common/zcHttp';

zcHttp.Post({
	IsNeedLogin: false,
	Paras: {
		Name: '',
		Action: '',
		Data: {
			Id: 3,
			Code: '001'
		}
	},
	Callback:fnXXX,
	Scope: this
});
或
let me = this;
me.$ZcPublic.Server.Post({
	IsNeedLogin: false,
	Paras: {
		Name: '',
		Action: '',
		Data: {
			Id: 3,
			Code: '001'
		}
	},
	Callback:fnXXX,
	Scope: this
});
// 写日志
me.$ZcPublic.Server.Post()
*/